package com.aistar.serviceVO.impl;

import com.aistar.mapper.ProgramMapper;
import com.aistar.mapper.ProgramRecordMapper;
import com.aistar.pojo.Program;
import com.aistar.pojo.ProgramRecord;
import com.aistar.pojo.ProgramRecordExample;
import com.aistar.pojoVO.ProgramRecordVO;
import com.aistar.serviceVO.ProgramRecordVOService;
import com.aistar.util.MessageUtil;
import com.aistar.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProgramRecordVOServiceImpl implements ProgramRecordVOService {
    private final ProgramMapper programMapper;
    private final ProgramRecordMapper programRecordMapper;

    @Autowired
    public ProgramRecordVOServiceImpl(ProgramMapper programMapper,
                                ProgramRecordMapper programRecordMapper) {
        this.programMapper = programMapper;
        this.programRecordMapper = programRecordMapper;
    }

    @Override
    public ServerResponse getByCustomerId(Integer customerId) {
        ProgramRecordExample example = new ProgramRecordExample();
        example.createCriteria()
                .andStatusEqualTo(MessageUtil.STATUS_SUCCESS)
                .andCustomerIdEqualTo(customerId);
        example.setOrderByClause("program_start_time desc, program_record_time desc");
        List<ProgramRecord> programRecordList = programRecordMapper.selectByExample(example);
        if (programRecordList != null && programRecordList.size() > 0) {
            List<ProgramRecordVO> programRecordVOList = new ArrayList<>();
            for (ProgramRecord programRecord : programRecordList) {
                Program program = programMapper.selectByPrimaryKey(programRecord.getProgramId());
                programRecordVOList.add(new ProgramRecordVO(programRecord, program));
            }
            return ServerResponse.getSuccess(programRecordVOList);
        }
        return ServerResponse.getFailed();
    }
}
